package sqlanalysisgui.view.model.provider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import sqlanalysisgui.session.SessionInfo;
import sqlanalysisgui.view.model.GraphNode;
import sqlanalysisgui.view.model.GraphNodeConnection;
import sqlanalysisgui.view.model.TableInfo;

import com.cho.core.db.dao.TableDao;
import com.cho.core.vo.TableVo;

public class GraphNodeModelContentProvider {
	  private List<GraphNodeConnection> connections;
	  private List<GraphNode> nodes;
	  public int displayHeightSize = 0;
	  
	  public GraphNodeModelContentProvider() {
			TableDao dao = new TableDao();
			List<Map<String, String>> tableList = dao.selectTableList(new TableVo(SessionInfo.getProjectInfo().getProjectName()));
			List<TableInfo> tableInfos = null;
			TableInfo tableInfo = null;
			GraphNode crudTypeNode = null;
			GraphNode crudAndFileNode = null;
			GraphNode crudAndFileAndSourceLineNode = null;
			String beforeFileName = "";
			int tableListSize = tableList.size();
			int noteTotalCount = 0;
			
			String[] crudName = new String[]{"C","R","U","D"};
			String[] crudDmlName = new String[]{"INSERT","SELECT","UPDATE","DELETE"};
			
		    // Image here a fancy DB access
		    // Now create a few nodes
		    nodes = new ArrayList<GraphNode>();
		    connections = new ArrayList<GraphNodeConnection>();
		    
			for (int i = 0; i < tableListSize; i++) {
				GraphNode tableNode = new GraphNode(i+"", tableList.get(i).get("TABLE_NAME_AND_COMMENT"));
				//System.out.println("a"+tableList.get(i).get("TABLE_NAME_AND_COMMENT"));
				nodes.add(tableNode);
				for (int crudIndex = 0; crudIndex < crudDmlName.length; crudIndex++) {
					beforeFileName = "";
					tableInfos = dao.selectFileAndTableList(new TableVo(SessionInfo.getProjectInfo().getProjectName(), tableList.get(i).get("TABLE_NAME"), crudName[crudIndex], "B.FILE_SEQ, "));
					for (int j = 0; j < tableInfos.size(); j++) {
						
						if(0 == j){
							//CRUD NODE 생성
							crudTypeNode = new GraphNode(j+"", crudDmlName[crudIndex]);
							nodes.add(crudTypeNode);
							connections.add(new GraphNodeConnection(j+"", j+"", tableNode,
									crudTypeNode));
						}
						
						tableInfo = tableInfos.get(j);
						
						if(!beforeFileName.equals(tableInfo.getFileName())){
							//FILE NODE 생성
							crudAndFileNode = new GraphNode(j + "_" + tableInfo.getFileName(), tableInfo.getFileName());
							nodes.add(crudAndFileNode);
							connections.add(new GraphNodeConnection(j + "_" + tableInfo.getFileName(), j + "_" + tableInfo.getFileName(), crudTypeNode,
									crudAndFileNode));
						}
						
						crudAndFileAndSourceLineNode = new GraphNode(j + "_" + tableInfo.getSourceLine(), tableInfo.getSourceLine() + " line");
						
						nodes.add(crudAndFileAndSourceLineNode);
						connections.add(new GraphNodeConnection(j + "_" + tableInfo.getSourceLine(), j + "_" + tableInfo.getSourceLine(), crudAndFileNode,
								crudAndFileAndSourceLineNode));
						noteTotalCount++;
						// GraphNode.getNodeFigure().addMouseListener()
						beforeFileName = tableInfo.getFileName();
					}
				} 
				
			}

	    // Because we are lasy we save the info about the connections in the
	    // nodes

	    for (GraphNodeConnection connection : connections) {
	      connection.getSource().getConnectedTo()
	          .add(connection.getDestination());
	    }
	    
	    displayHeightSize = noteTotalCount * 35;
	  }

	  public List<GraphNode> getNodes() {
	    return nodes;
	  }

	  
	  
	  public void GraphNodeModelContentProvider() {
		    // Image here a fancy DB access
		    // Now create a few nodes
		    nodes = new ArrayList<GraphNode>();
		    GraphNode node = new GraphNode("1", "Hamburg");
		    nodes.add(node);
		    node = new GraphNode("2", "Frankfurt");
		    nodes.add(node);
		    node = new GraphNode("3", "Berlin");
		    nodes.add(node);
		    node = new GraphNode("4", "Munich");
		    nodes.add(node);
		    node = new GraphNode("5", "Eppelheim");
		    nodes.add(node);
		    node = new GraphNode("6", "Ahrensboek");
		    nodes.add(node);

		    connections = new ArrayList<GraphNodeConnection>();
		    GraphNodeConnection connect = new GraphNodeConnection("1", "1", nodes.get(0),
		        nodes.get(1));
		    connections.add(connect);
		    connect = new GraphNodeConnection("2", "2", nodes.get(0), nodes.get(4));
		    connections.add(connect);
		    connect = new GraphNodeConnection("3", "3", nodes.get(2), nodes.get(1));
		    connections.add(connect);
		    connect = new GraphNodeConnection("4", "3", nodes.get(1), nodes.get(3));
		    connections.add(connect);

		    // Because we are lasy we save the info about the connections in the
		    // nodes

		    for (GraphNodeConnection connection : connections) {
		      connection.getSource().getConnectedTo()
		          .add(connection.getDestination());
		    }
		  }
}
